﻿// -----------------------------------------------------------------------
// <copyright file="PhieuChanDoanHinhAnhChiTietDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class PhieuChanDoanHinhAnhChiTietDataAccess
    {
        public bool InsertPhieuChanDoanHinhAnhChiTiet(PhieuChanDoanHinhAnhChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuChanDoanHinhAnhChiTiet_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChanDoanHinhAnh", DbType.String, obj.MaChanDoanHinhAnh);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertPhieuChanDoanHinhAnhChiTiet", ex.Message); throw ex; }
        }

        public bool EditPhieuChanDoanHinhAnhChiTiet(PhieuChanDoanHinhAnhChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuChanDoanHinhAnhChiTiet_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChanDoanHinhAnh", DbType.String, obj.MaChanDoanHinhAnh);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdatePhieuChanDoanHinhAnhChiTiet", ex.Message); throw ex; }
        }

        public bool DeletePhieuChanDoanHinhAnhChiTiet(string maChanDoanHinhAnh, string machitieu)
        {
            try
            {
                string sql = "spPhieuChanDoanHinhAnhChiTiet_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChanDoanHinhAnh", DbType.String, maChanDoanHinhAnh);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, machitieu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuChanDoanHinhAnhChiTiet", ex.Message); throw ex; }
        }

        public bool DeletePhieuChanDoanHinhAnhChiTiet_ByMaPhieuChanDoanHinhAnh(string maPhieuChanDoan)
        {
            try
            {
                string sql = "[dbo].[spPhieuChanDoanHinhAnhChiTiet_DeleteByMaChanDoanHinhAnh]";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChanDoanHinhAnh", DbType.String, maPhieuChanDoan);

                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuChanDoanHinhAnhChiTiet_ByMaPhieuChanDoanHinhAnh", ex.Message); throw ex; }
        }

        public PhieuChanDoanHinhAnhChiTietEntity GetPhieuChanDoanHinhAnhChiTiet(string maPhieuChanDoan, string maChiTieu)
        {
            try
            {
                PhieuChanDoanHinhAnhChiTietEntity obj = null;
                string sql = "spPhieuChanDoanHinhAnhChiTiet_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChanDoanHinhAnh", DbType.String, maPhieuChanDoan);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuChanDoanHinhAnhChiTietEntity();
                        obj.MaChanDoanHinhAnh = reader["MaChanDoanHinhAnh"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuChanDoanHinhAnhChiTiet", ex.Message); throw ex; }
        }

        public List<PhieuChanDoanHinhAnhChiTietEntity> GetPhieuChanDoanHinhAnhChiTietList()
        {
            try
            {
                List<PhieuChanDoanHinhAnhChiTietEntity> list = new List<PhieuChanDoanHinhAnhChiTietEntity>();
                PhieuChanDoanHinhAnhChiTietEntity obj = null;
                string sql = "spPhieuChanDoanHinhAnhChiTiet_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuChanDoanHinhAnhChiTietEntity();
                        obj.MaChanDoanHinhAnh = reader["MaChanDoanHinhAnh"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuChanDoanHinhAnhChiTietList", ex.Message); throw ex; }
        }

        public List<PhieuChanDoanHinhAnhChiTietEntity> GetPhieuChanDoanHinhAnhChiTietByMaChanDoanHA(string maChanDoanHinhAnh)
        {
            try
            {
                List<PhieuChanDoanHinhAnhChiTietEntity> list = new List<PhieuChanDoanHinhAnhChiTietEntity>();
                PhieuChanDoanHinhAnhChiTietEntity obj = null;
                string sql = "spPhieuChanDoanHinhAnhChiTiet_SelectByMaChanDoan";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChanDoanHinhAnh", DbType.String, maChanDoanHinhAnh);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuChanDoanHinhAnhChiTietEntity();
                        obj.MaChanDoanHinhAnh = reader["MaChanDoanHinhAnh"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuChanDoanHinhAnhChiTietByMaChanDoanHA", ex.Message); throw ex; }
        }

    }
}